﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMN.Models;
using CMN.Entity;
using CMN.Config;
using CMN.Common;
using CMN.Models.Interfaces;
using System.Web;
using CMN.BLL;
using System.Threading;
using System.IO;
namespace CMN.Domain
{
    public partial class Form1 : Form
    {
        private AsyncUpdateUrlLog m_AsyncImport;
        private bool formLoadOk = false;
        public Form1()
        {
            
            InitializeComponent();
           
            //QueryParam qp = new QueryParam();
            //qp.ReturnFields = "*  ";
            
           // bingGridSite(qp);
            formLoadOk = true;
           
        }
        private void bingGridSite(QueryParam qp)
        {
            qp.PageSize = 1000;
            gridSite.Rows.Clear();
            DomainsEntity site = new DomainsEntity();
            IDataService<DomainsEntity> siteSv = ServiceBuilder.BuildDomainsService();
            int recordNum = 0;
            List<DomainsEntity> siteList = siteSv.List(qp, out recordNum);
            int index = 0;
            DataGridViewRow Row = null;
            int indexNum = 0;
            foreach (DomainsEntity info in siteList)
            {
                index = this.gridSite.Rows.Add();
                Row = this.gridSite.Rows[index];
                Row.Cells["ID"].Value = info.ID.ToString();
                Row.Cells["SiteName"].Value = info.SiteName;
                Row.Cells["SiteUrl"].Value = info.SiteUrl;

                Row.Cells["BR"].Value = info.BR.ToString();
                Row.Cells["IndexNum"].Value = info.IndexNum.ToString();
                Row.Cells["OutUrlNum"].Value = info.OutUrlNum.ToString();
                Row.Cells["DeleteTime"].Value = info.DeleteTime.ToString();
                Row.Cells["Mark"].Value = info.Mark;
                Row.Cells["PR"].Value = info.PR.ToString();
                Row.Cells["SiteType"].Value = info.SiteType;
            }
            DataGridViewTextBoxColumn dgv_Text = new DataGridViewTextBoxColumn();
            for (int i = 0; i < gridSite.Rows.Count; i++)
            {
                int j = i + 1;
                gridSite.Rows[i].HeaderCell.Value = j.ToString();
            }
            
        }
        private void gridSite_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && this.gridSite.Rows.Count > e.RowIndex)
            {
                frmAddSite frmUrl = new frmAddSite(Utils.StringToInt(this.gridSite.Rows[e.RowIndex].Cells[0].Value.ToString(), 0));

                frmUrl.Show();
            }
        }

        private void tsMenuAddSite_Click(object sender, EventArgs e)
        {
            frmAddSite addSite = new frmAddSite();
            addSite.Show();
        }

        private void tsMenuAddUrl_Click(object sender, EventArgs e)
        {

        }

        private void btnUpdateIndex_Click(object sender, EventArgs e)
        {

            BLLHelper bllHelper = new BLLHelper();
           
            this.m_AsyncImport = new AsyncUpdateUrlLog(this);
            this.m_AsyncImport.TaskProgress += new EventHandler(AsyncImport_TaskProgress);
            this.m_AsyncImport.TaskDone += new EventHandler(AsyncImport_TaskDone);
            m_AsyncImport.StrWhere ="( indexNum='' and outUrlNum='') and "+ getWhere();
            m_AsyncImport.Start();
            btnUpdateIndex.Enabled = false;
        }
        void AsyncImport_TaskDone(object sender, EventArgs e)
        {
            btnUpdateIndex.Enabled = true;
            lbUpdate.Text = "完成";
        }
        void AsyncImport_TaskProgress(object sender, EventArgs e)
        {
            AsyncEventArgs pAsyncEventArgs = e as AsyncEventArgs;
            int step = (int)pAsyncEventArgs.getObjects[0];
            lbUpdate.Text = "共" + pAsyncEventArgs.action.ToString() + "当前第" + (step + 1).ToString();

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Enabled = false;
            QueryParam qp = new QueryParam();

            qp.Where = getWhere();
            qp.Orderfld = " outUrlNum desc ";
            bingGridSite(qp);
            btnSearch.Enabled = true;
        }

        private string getWhere()
        {
            BLLHelper bllHelper = new BLLHelper();
              string strWhere=string.Empty;
              if (!ckb_IsSearchTime.Checked)
              {
                  string strDateStart = bllHelper.GetSqlDateTime(Utils.StringToDataTime(dateStart.Text));
                  string strDateEnd = bllHelper.GetSqlDateTime(Utils.StringToDataTime(dateEnd.Text));
                  strWhere = string.Concat("DeleteTime >=", strDateStart, " and DeleteTime<=", strDateEnd, "  ");
              }
              if (!string.IsNullOrEmpty(tbKeyWord.Text))
              {
                  if (!ckb_IsSearchTime.Checked)
                      strWhere = strWhere + " and ";
                  strWhere = strWhere + "   siteUrl like '%" + tbKeyWord.Text + "%' ";

              }
            return  strWhere;
        }
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            //停止工作线程
            if (this.m_AsyncImport != null)
                this.m_AsyncImport.Stop();
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }



        private void gridSite_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (e.ColumnIndex == 2)
                {
                    System.Diagnostics.Process.Start("chrome.exe", this.gridSite.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                }
                if (e.ColumnIndex == 3 || e.ColumnIndex == 4)
                {
                    string url = string.Concat("http://www.baidu.com/s?wd=%22www.", this.gridSite.Rows[e.RowIndex].Cells[2].Value.ToString().Replace("http://", ""), "%22");

                    System.Diagnostics.Process.Start("chrome.exe", url);
                }
                if (e.ColumnIndex == 4)
                {
                    string url = string.Concat("http://www.baidu.com/s?wd=site%3A", this.gridSite.Rows[e.RowIndex].Cells[2].Value.ToString().Replace("http://", ""));
                    System.Diagnostics.Process.Start("chrome.exe", url);
                }
            }
        }
    

        

        private void gridSite_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
           
            if (formLoadOk)
            {
                QueryParam qp = new QueryParam();
                qp.Where = getWhere();
                if(gridSite.Columns[e.ColumnIndex].Name=="IndexNum" ||gridSite.Columns[e.ColumnIndex].Name=="OutUrlNum" ){

                    
                qp.Orderfld = string.Concat(" convert(int,",gridSite.Columns[e.ColumnIndex].Name, ") desc ");
                }
                else 
                {
                    qp.Orderfld = string.Concat(" ",gridSite.Columns[e.ColumnIndex].Name, " desc ");
                }
                bingGridSite(qp);
            }
        }

        private void gridSite_Sorted(object sender, EventArgs e)
        {

        }

        
    }
}
